package uit.edu.vn.portal.elearning.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import uit.edu.vn.portal.elearning.support.WebServiceSupport;
import uit.edu.vn.portal.elearning.utils.XmlParser;
import uit.edu.vn.portal.elearning.models.schedule.CalendarEvent;

public class ScheduleWSLogic {
	private String authenUserName;
	private String authenPassword;
	private String loginUrl;
	private String serviceUrl;
	private String scriptUrl;

	private String eid;
	
	// stores the admin session, not accessible
	private String session;

	private final String METHOD_LOGIN = "login";
	private final String METHOD_CHECK_SESSION = "checkSession";
	private final String METHOD_GET_SCHEDULESFORSITE = "getSchedulesForSite";
	
	public ScheduleWSLogic() {
		// TODO Auto-generated constructor stub
	}
	
	public ScheduleWSLogic(String authenUserName, String authenPassword, String loginUrl, String scriptUrl, String serviceUrl){
			setAuthenUserName(authenUserName);
			setAuthenPassword(authenPassword);
			setLoginUrl(loginUrl);
			setScriptUrl(scriptUrl);
			setServiceUrl(serviceUrl);
	}
	
	/**
	 * Get a new session for the admin user. Don't call this directly, use getSession() instead.
	 * @return
	 */
	private String getUserSession() {
		
		String session = null;
		
		//setup data to send
		Map<String,Map<String,String>> data = new HashMap<String,Map<String,String>>();
		data.put("id", new HashMap<String,String>(){
			{
				put("value",getAuthenUserName());
				put("type","string");
			}
		});
		data.put("pw", new HashMap<String,String>(){
			{
				put("value",getAuthenPassword());
				put("type","string");
			}
		});
		
		session = WebServiceSupport.call(getLoginUrl(), METHOD_LOGIN, data);
		
		//store locally
		setSession(session);
		
		//and return it
		return session;
	}
	
	public List<CalendarEvent> getSchedulesForSite(String siteid){
		Map<String,Map<String,String>> data = new HashMap<String,Map<String,String>>();
		data.put("sessionid", new HashMap<String,String>(){
			{
				put("value",getSession());
				put("type","string");
			}
		});
		
		HashMap<String, String> site_params = new HashMap<String, String>();
		site_params.put("value", siteid);
		site_params.put("type", "string");
		data.put("siteid", site_params);
		
		String xml = WebServiceSupport.call(getServiceUrl(), METHOD_GET_SCHEDULESFORSITE, data);		
		return XmlParser.parseListOfCalendarEvents(xml);
	}
	
	/**
	 * Check the current session is still active. Don't call this directly, use getSession() instead.
	 * @return
	 */
	private boolean isSessionActive() {
		
		Map<String,Map<String,String>> data = new HashMap<String,Map<String,String>>();
		data.put("sessionid", new HashMap<String,String>(){
			{
				put("value",session);
				put("type","string");
			}
		});
		
		String results = WebServiceSupport.call(getScriptUrl(), METHOD_CHECK_SESSION, data);
		
		if(StringUtils.equals(results, session)) {
			return true;
		}
		
		return false;
	}
	
	public String getAuthenUserName() {
		return authenUserName;
	}

	public void setAuthenUserName(String authenUserName) {
		this.authenUserName = authenUserName;
	}

	public String getAuthenPassword() {
		return authenPassword;
	}

	public void setAuthenPassword(String authenPassword) {
		this.authenPassword = authenPassword;
	}

	public String getLoginUrl() {
		return loginUrl;
	}
	
	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}
	
	public String getServiceUrl() {
		return serviceUrl;
	}
	
	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}
	
	public String getScriptUrl() {
		return scriptUrl;
	}

	public void setScriptUrl(String scriptUrl) {
		this.scriptUrl = scriptUrl;
	}

	public void setEid(String eid) {
		this.eid = eid;
	}
	public String getEid() {
		return eid;
	}

	/**
	 * Get local session, check it's still active, otherwise get a new one
	 * @return
	 */
	private String getSession() {
		
		if(StringUtils.isBlank(session)) {
			return getUserSession();
		}
		
		if(!isSessionActive()){
			return getUserSession();
		}
		
		return session;
	}
	
	private void setSession(String session) {
		this.session = session;
	}
}
